﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Microsoft.WowAddonStudio.Lua.Loaders
{
    public class DirectoryXmlDocumentationLoader : XmlDocumentationLoader
    {
        private readonly string rootPath;
        private readonly string wildcard;

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlDocumentationLoader"/> class.
        /// </summary>
        /// <param name="rootPath">The root path where the XML files reside.</param>
        /// <param name="wildcard">The wildcard for the XML files.</param>
        public DirectoryXmlDocumentationLoader(string rootPath, string wildcard)
        {
            if (rootPath == null)
                throw new ArgumentNullException("rootPath");
            if (wildcard == null)
                throw new ArgumentNullException("wildcard");

            this.rootPath = rootPath;
            this.wildcard = wildcard;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlDocumentationLoader"/> class.
        /// </summary>
        /// <param name="rootPath">The root path where the XML files reside.</param>
        public DirectoryXmlDocumentationLoader(string rootPath)
            : this(rootPath, "*.xml")
        {
        }

        /// <summary>
        /// Gets the streams containing the XML documentation.
        /// </summary>
        /// <returns>
        /// An enumerable collection of <see cref="Stream"/>s.
        /// </returns>
        protected override IEnumerable<Stream> GetDocumentationStreams()
        {
            return Directory.GetFiles(rootPath, wildcard, SearchOption.TopDirectoryOnly)
                .Select(path => File.Open(path, FileMode.Open, FileAccess.Read)).Cast<Stream>();
        }
    }
}
